from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from starlette.middleware.sessions import SessionMiddleware  # ✅ 关键导入
from auth.db import Base, engine
from auth import routers as auth_router
from devices import routers as device_router
from ecg import routers as ecg_router
from auth import profile_router
from auth import user_router


# 创建 FastAPI 应用
app = FastAPI(title="可穿戴心电监测平台")

# ✅ 添加 Session 中间件（验证码依赖）
app.add_middleware(SessionMiddleware, secret_key="supersecretkey123")

# 静态文件
app.mount("/static", StaticFiles(directory="static"), name="static")

# 初始化数据库
Base.metadata.create_all(bind=engine)
from auth.db import SessionLocal
from auth import models, utils

def init_superadmin():
    db = SessionLocal()
    if not db.query(models.User).filter(models.User.role == "superadmin").first():
        superadmin = models.User(
            username="root",
            password=utils.hash_password("111111"),
            email="superadmin@system.com",
            phone="13900000000",
            role="superadmin"
        )
        db.add(superadmin)
        db.commit()
    db.close()
init_superadmin()
# 注册所有路由
app.include_router(auth_router.router)
app.include_router(device_router.router)
app.include_router(ecg_router.router)
app.include_router(profile_router.router)

app.include_router(user_router.router)


